/**
 * 查勘员定位，轨迹回放用到： 输入查询条件下 显示如google、百度查询效果一样，自动查出内容供选择
 */
$(function() {
	$("#username" ).autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: getUrl(),
				data: getDataJson(),
				dataType: "json",
				success: function( data ) {
					response( $.map(data, function( item ) {
						return {
							label: item.name + ","+item.userNumber,
							value: item.name
						}
					}));
				}
			});
		}
	});
});

$(function() {
	$("#userNumber" ).autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: getUrl(),
				data: getDataJson(),
				dataType: "json",
				success: function( data ) {
					response( $.map(data, function( item ) {
						return {
							label: item.name + ","+item.userNumber,
							value: item.userNumber
						}
					}));
				}
			});
		}
	});
});

$(function() {
	$("#phone" ).autocomplete({
		source: function( request, response ) {
			$.ajax({
				url: getUrl(),
				data: getDataJson(),
				dataType: "json",
				success: function( data ) {
					response( $.map(data, function( item ) {
						return {
							label: item.phoneSim,
							value: item.phoneSim
						}
					}));
				}
			});
		}
	});
});

/**
 * 统一一个url地址供autocomplete使用
 */
 
function getUrl(){
	return "/survey/rest/locate/surveysPosition";
}


function getSimpleMsg(userLocation){
	
	var msg = "<div height='1px'/>";
	msg += "工号:" + userLocation.userNumber+"<br>";
	msg += "名字：" + userLocation.name + "<br>";
	msg += "电话号码：" + userLocation.phoneSim + "<br>";
	var place = userLocation.place.split(",");
	var point =new BMap.Point(place[0], place[1]);
	var gc = new BMap.Geocoder();  //反向地址解析器
	var str="<span id='location_zh'></span>";
	// TODO 如果异步的速度快于页面展示的速度，则些处处理有问题。
	gc.getLocation(point, function(rs){
		$("#location_zh").html(rs.address);
    }); 
	
	msg += "所在地点：" + "<br>";
	msg += "&nbsp;&nbsp;" +str+"<br>";
	msg += "更新时间：" + userLocation.timeStr + "<br>";
//	msg += "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	if(userLocation.userState === "-1"){
		msg += "<div height='1px'/>状态：" + "空闲";
	} else {
		msg += "<div height='1px'/>状态：" + "任务中";
	}
	//添加呼叫按钮
	msg +="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class='yj_button2'>呼叫</a>"+ "<br>"
		
	return msg;
}

/**
 * 得到查勘员信息(定位)
 */
function getUserMsg(userLocation) {
	var sContent = "<div width='200px'>" + 
			"<h4 style='margin:0 0 5px 0;padding:0.2em 0'>查勘员信息</h4>" + 
			"<img style='float:right;margin:4px' id='imgDemo' src='/survey/img/survey.png' width='100' height='104' />" + 
			"<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>"+
				getSimpleMsg(userLocation) +
			"</p>" + 
			"</div>";
	return sContent;
}
function getDistUserMsg(userLocation){
	var sContent = "<div width='200px'>" + 
		"<h4 style='margin:0 0 5px 0;padding:0.2em 0'>查勘员信息</h4>" + 
		"<img style='float:right;margin:4px' id='imgDemo' src='/survey/img/survey.png' width='100' height='104' />" + 
		"<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>"+
			getSimpleMsg(userLocation) +
			"<form action='/survey/rest/estimate/distribute' method='post'>"+
			"<br>此查勘员指派给报案号:&nbsp;<input id='disreporter' type='text' name='disreporter' class='yj_othfld4' />" +
			"&nbsp;&nbsp<a class='yj_button2' id='distribut' onclick='sendtask(\""+userLocation.userNumber+"\",0)'>指派</a>"+
			"</form></p>" + 
		"</div>";
	return sContent;
}
/**
 * 得到查勘员信息(列表分发查勘任务)
 * 查勘员中指定了报案号
 * @param userLocation
 * @returns {String}
 */
function getSingleDistUserMsg(userLocation){
	var sContent = "<div width='200px'>" +
		"<h4 style='margin:0 0 5px 0;padding:0.2em 0'>查勘员信息</h4>" + 
		"<img style='float:right;margin:4px' id='imgDemo' src='/survey/img/survey.png' width='100' height='104' />" + 
		"<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>"+
			getSimpleMsg(userLocation) +
			"<form action='/survey/rest/estimate/distribute' method='post'>"+
			"<br>此查勘员指派给报案号:&nbsp;<input id='disreporter' class='yj_othfld4' type='text' name='disreporter' readonly=true value='"+window.dispatchObj.reporterNumber+"'/>" +
			"&nbsp;&nbsp;<a class='yj_button2'  id='distribut' onclick='sendtask(\""+userLocation.userNumber+"\",1)'>指派</a>"+
			"</form></p>" + 
		"</div>";
	return sContent;
}

/**
 * 得到出险位置的信息
 * 
 * @param estimateLocation
 * @returns {String}
 */
function getReporterMsg(ReporterLocation,nearestId,distance){
	var worksurveylist = window.worksurvey;
	window.ReporterLocation = ReporterLocation;
	var msg = "";
	msg += "<div height='1px'/>报案号:" + ReporterLocation.reporterNumber + "<br>";
	msg += "车牌号:" + ReporterLocation.carNumber + "<br>";
	
	msg += "出险地点: " + ReporterLocation.placeName + "<br>";
	msg += "报案时间: " + ReporterLocation.reporterTime + "<br>";
	msg += "当前状态: 未处理<br>";
	msg += "离此最近的查勘员(工号:" + nearestId+"),相距:"+distance+"公里";
	var sContent = "<div width='200px'>" + 
					"<h4 style='margin:0 0 5px 0;padding:0.2em 0'>出险信息</h4>" + 
					"<img style='float:right;margin:4px' id='imgDemo' src='/survey/img/car.png' width='100' height='104' />" + 
					"<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>"+
						msg +
					"<form action='/survey/rest/estimate/distribute' method='post'>"+
					"<br>该任务分发给查勘人员:&nbsp;<select id='usernumber' name='usernumber' class='yj_select' />" ;
	for(var index = 0; index < worksurveylist.length; index ++ ){
		sContent += "<option value='"+worksurveylist[index].userNumber+"'>"+worksurveylist[index].name+"("+worksurveylist[index].userNumber+")</option>" ;
	}
	sContent +=	"</select>"+
					"&nbsp;<a class='yj_button2' id='distribut' onclick='dispatchmap()'>分发</a>"
					"</form></p>" + 
					"</div>";
	return sContent;
}


function dispatchmap(){
	$.ajax({
		url: "/survey/rest/task/mapdistribute",
		type: "GET",
		data: getEstimateDate(),
		success: function(result, status){
				if(result == "0") {	
					alert("分发成功");
				} else {
					alert(result);
				}
				if(window.flag==0){
					flush();
				//	$("#taskList").click();
				}else {
					//关闭dialog
					$(".ui-icon").click();
					//刷新列表
					search();
				}
         },
         error: function(){
             alert("分发失败");
         }
	});	
}

function getEstimateDate(){
	
	return {
		"reporterId": ReporterLocation.id ,
		"reporterNumber": ReporterLocation.reporterNumber,
		"carNumber": ReporterLocation.carNumber,
		"userNumber": $("#usernumber").val(),
		"estimatePlace": ReporterLocation.place,
		"relationNumber": ReporterLocation.relationNumber,
		"identify": ReporterLocation.identify
	}
}

function sendtask(userNumber,flag){
	$.ajax({
		url: "/survey/rest/task/sendtask",
		type: "GET",
		data: {
			"userNumber": userNumber,
			"reporterNumber": $("#disreporter").val()
		},
		dataType: 'json',
		success: function(result, status){
				alert("指派成功");
				if(window.flag==0){
					flush();
				//	$("#taskList").click();
				}else {
					//关闭dialog
					$(".ui-icon").click();
					//刷新列表
					search();
				}
         },
         error: function(result, status){
        	 if(status=="parsererror")
        		 alert("查勘员工号或报案号错误");
        	 else
        		 alert("send task fail");
        	 
        	 if(window.flag==0){
					flush();
				}else {
					$(".ui-icon").click();
					search();
				}
         }
	});
}
